home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Sessions / Traut / ZStrings / Source / Win32 / Win32ZStringOverrideTool.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  3.8 KB  |  151 lines

  1. /*==================================================================
  2.     File:        Win32ZStringExtractTool.cpp
  3.  
  4.     Contains:    Tool for creating override libraries for
  5.                 ZStrings on the  Windows
  6.  
  7.     Written by:    Nalini Prakash
  8.  
  9.     Copyright:    2000-2001 Connectix Corporation
  10.     
  11.     This source has been placed into the public domain by
  12.     Connectix Corporation. You have the right to modify, 
  13.     distribute or use this code without any legal limitations
  14.     or finanicial/licensing requirements. Connectix is not 
  15.     liable for any problems that result from the use of this 
  16.     code.
  17.     
  18.     If you have comments, feedback, questions, or would like
  19.     to submit bug fixes or updates to this code, please email
  20.     opensource@connectix.com.
  21. ==================================================================*/
  22.  
  23. #include "StdAfx.h"
  24.  
  25. #include "ZStringTypes.h"
  26. #include "ZStringTool.h"
  27.  
  28. #include "Win32ZStringTools.h"
  29. #include "Win32ZStringOverrideTool.h"
  30.  
  31. #ifdef _DEBUG
  32. #undef THIS_FILE
  33. static char THIS_FILE[]=__FILE__;
  34. #define new DEBUG_NEW
  35. #endif
  36.  
  37.  
  38. char *    Win32ZStringOverrideTool::sNewBinaryTitle = "Please Select New Binary File";
  39. char *    Win32ZStringOverrideTool::sReportFileTitle = "Please Select Report File";
  40.  
  41.  
  42. //////////////////////////////////////////////////////////////////////
  43. // Construction/Destruction
  44. //////////////////////////////////////////////////////////////////////
  45.  
  46. Win32ZStringOverrideTool::Win32ZStringOverrideTool()
  47. {
  48. }
  49.  
  50. Win32ZStringOverrideTool::~Win32ZStringOverrideTool()
  51. {
  52. }
  53.  
  54.  
  55. bool
  56. Win32ZStringOverrideTool::CreateOverrideDictionary(
  57.         CString                    inSrcFile,
  58.         CString                    inDestFile,
  59.         ZToolOptions            inOptions)
  60. {
  61.     HANDLE            overrideFile = INVALID_HANDLE_VALUE;
  62.     HANDLE            memOverride = NULL;
  63.     char *            overrideFileImage = NULL;
  64.     Z_UInt32        overrideFileSize = 0;
  65.  
  66.     // Open as a memory mapped file.
  67.     if (!::OpenMemMappedFile(inSrcFile, overrideFile, memOverride,
  68.             overrideFileImage, overrideFileSize)) 
  69.     {
  70.         ::AfxMessageBox("Unable to open the source file.\n Override will not be completed.");
  71.         return false;
  72.     }
  73.  
  74.     CWaitCursor wait;
  75.  
  76.     // Process the data.
  77.     ZStringTool        stringTool;
  78.     stringTool.ProcessBinaries(overrideFileImage, overrideFileSize, NULL, 0, inOptions);
  79.  
  80.     FILE * errorFile = fopen("errors.html", "w+");
  81.     if (errorFile == NULL) 
  82.         ::AfxMessageBox("Unable to open the error file.\n Any errors will not be reported.");
  83.     else {
  84.         Z_Boolean isOk = stringTool.PrintReport(errorFile, inOptions);
  85.         fclose(errorFile);
  86.  
  87.         if (!isOk) {
  88.             ::ShellExecute(NULL, "open", "errors.html", NULL, NULL, SW_SHOWNORMAL);
  89.             ::AfxMessageBox("Errors have been found.  These need fixed before the override dictionary will print.\n");
  90.             return false;
  91.         }
  92.     }
  93.  
  94.     if (overrideFile != INVALID_HANDLE_VALUE)
  95.         ::CloseFiles(overrideFileImage, memOverride, overrideFile);
  96.  
  97.     char *            dictionary = NULL;
  98.     Z_UInt32        dictLength;
  99.     
  100.     Z_Boolean success = stringTool.CreateOverrideDictionary(dictionary, dictLength);
  101.  
  102.     if (!success) 
  103.     {
  104.         ::AfxMessageBox("Unable to allocate the dictionary.\n Override will not be completed.");
  105.         return false;
  106.     }
  107.     
  108.     char *        newResHandle = new char[dictLength];
  109.  
  110.     if (newResHandle == NULL)
  111.     {
  112.         ::AfxMessageBox("Unable to allocate a new resource.\n Override will not be completed.");
  113.         return false;
  114.     }
  115.  
  116.     memcpy(newResHandle, dictionary, dictLength);
  117.  
  118.     try
  119.     {
  120.         CFile                reportFile;
  121.         CString                reportFileName = inDestFile;
  122.  
  123.         // Delete any existing file, and create a new override file.
  124.         reportFile.Open(reportFileName, CFile::modeCreate |   
  125.           CFile::modeReadWrite);
  126.     
  127.         reportFile.Write(newResHandle, dictLength);
  128.  
  129.         reportFile.Close();        // if omitted, destuctor will close.
  130.     }
  131.     catch (CFileException * exp)
  132.     {
  133.         // Display error message.
  134.         exp->ReportError();
  135.         exp->Delete();
  136.           if (dictionary != NULL)
  137.             free(dictionary);
  138.         delete [] newResHandle;
  139.         return false;
  140.        }
  141.  
  142.     ::AfxMessageBox("Override dictionary printed successfully.\n");
  143.  
  144.     delete [] dictionary;
  145.     delete [] newResHandle;
  146.  
  147.     return true;
  148. }
  149.  
  150.  
  151.